package com.jkj.http.sys.controller;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jkj.http.controller.BaseController;
import com.jkj.http.entity.LayPadding;
import com.jkj.http.entity.TreeSelect;
import com.jkj.http.interceptor.AuthorizeChecked;
import com.jkj.http.sys.entity.SysOrganize;
import com.jkj.http.sys.entity.SysUser;
import com.jkj.http.sys.service.SysOrganizeService;
import com.jkj.utils.JsonUtils;
import com.jkj.utils.OperatorProvider;
import com.jkj.utils.TreeSelectHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;


@RestController
@RequestMapping(value = "/system/organize")
public class OrganizeController extends BaseController {
    @Autowired
    private SysOrganizeService organizeService;

    /**
     * 组织部门列表画面
     *
     * @return
     */
    @AuthorizeChecked()
    @RequestMapping(value = "/index", method = {RequestMethod.GET})
    public ModelAndView index() {
        return new ModelAndView("system/organize/index");
    }

    /**
     * 查询组织信息
     *
     * @param pageIndex
     * @param pageSize
     * @param keyWord
     * @return
     */
    @AuthorizeChecked()
    @RequestMapping(value = "/index", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    public String index(int pageIndex, int pageSize, String keyWord) {
        int totalCount = organizeService.getTotalCount(keyWord);
        PageHelper.startPage(pageIndex, pageSize);//这行是重点，表示从pageNum页开始，每页pageSize条数据
        List<SysOrganize> list = organizeService.getList(keyWord);
        PageInfo<SysOrganize> pageInfo = new PageInfo<>(list);
        List<SysOrganize> pageData = pageInfo.getList();
        LayPadding<SysOrganize> result = new LayPadding<>();
        result.setResult(true);
        result.setMsg("success");
        result.setList(pageData);
        result.setCount(totalCount);
        return JsonUtils.toJSON(result);
    }

    /**
     * 新增或者修改组织画面
     *
     * @return
     */
    @AuthorizeChecked()
    @RequestMapping(value = "/form", method = {RequestMethod.GET})
    public ModelAndView form() {
        return new ModelAndView("system/organize/form");
    }

    /**
     * 插入修改组织信息
     *
     * @param session
     * @param model
     * @return
     */
    @AuthorizeChecked()
    @RequestMapping(value = "/form", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    public String form(HttpSession session, SysOrganize model) {
        if (StrUtil.isEmpty(model.getId())) {
            model.setCreateUser(OperatorProvider.getCurrent(session).getAccount());
            int row = organizeService.insert(model);
            return row > 0 ? success() : error();
        } else {
            if (model.getParentId() == model.getId()) {
                model.setParentId("0");
            }
            model.setModifyUser(OperatorProvider.getCurrent(session).getAccount());
            int row = organizeService.update(model);
            return row > 0 ? success() : error();
        }
    }

    /**
     * 根据主键获取组织信息
     *
     * @param primaryKey
     * @return
     */
    @RequestMapping(value = "/getForm", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    public String getForm(String primaryKey) {
        SysOrganize entity = organizeService.get(primaryKey);
        return JsonUtils.toJSON(entity);
    }

    /**
     * 删除组织信息
     *
     * @param primaryKey
     * @return
     */
    @AuthorizeChecked()
    @RequestMapping(value = "/delete", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    public String delete(String primaryKey) {
        int count = organizeService.getChildCount(primaryKey);
        if (count == 0) {
            int row = organizeService.delete(primaryKey);
            return row > 0 ? success() : error();
        }
        return error("操作失败，请先删除该项的" + count + "个子级机构。");
    }

    /**
     * 组织部门详情画面
     *
     * @return
     */
    @AuthorizeChecked()
    @RequestMapping(value = "/detail", method = {RequestMethod.GET})
    public ModelAndView detail() {
        return new ModelAndView("System/Organize/Detail");
    }

    /**
     * 获取组织信息
     *
     * @return
     */
    @RequestMapping(value = "/getListTreeSelect", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    public String getListTreeSelect() {
        List<SysOrganize> data = organizeService.getOrganizeList();
        List<TreeSelect> treeList = new ArrayList<TreeSelect>();
        for (SysOrganize item : data) {
            TreeSelect model = new TreeSelect();
            model.setId(item.getId());
            model.setText(item.getFullName());
            model.setParentId(item.getParentId());
            treeList.add(model);
        }
        return TreeSelectHelper.ToTreeSelectJson(treeList);
    }
}
